<template>
<div>
    <el-form :model="adminFrom" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="账号" prop="adminUsername">
            <el-input type="text" v-model="adminFrom.adminUsername" @blur="checkName" ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="adminPassword">
            <el-input type="password" v-model="adminFrom.adminPassword"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="checkPass">
            <el-input type="password" v-model="adminFrom.checkPass" ></el-input>
        </el-form-item>
        <el-form-item label="权限" prop="roleId">
            <el-select v-model="value" placeholder="请选择">
                <el-option
                    v-for="item in options"
                    :key="item.adminRolesname"
                    :label="item.adminRolesname"
                    :value="item.id">
                </el-option>
            </el-select>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
            <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
    </el-form>

</div>
</template>

<script>
export default {
    data() {
        var validaterolre = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('权限不能为空'));
            }
            setTimeout(() => {
                if (!Number.isInteger(value)) {
                    callback(new Error('请选择权限'));
                } else {
                        callback();
                }
            }, 1000);
        };
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.adminFrom.adminPassword !== '') {
                    this.$refs.ruleForm.validateField('checkPass');
                }
                callback();
            }
        };
        var validateName = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入账号'));
            } else {
                if (this.adminFrom.adminUsername !== '') {
                    this.$refs.ruleForm.validateField('adminUsername');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.adminFrom.adminPassword) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            adminFrom: {
                adminUsername: '',
                adminPassword: '',
                checkPass:'',
                roleId:''
            },
            value:'',
            id:this.$route.query.id,
            options: [{}],
            rules: {
                adminUsername: [
                    { validator: validateName, trigger: 'blur' }
                ],
                adminPassword: [
                    { validator: validatePass, trigger: 'blur' }
                ],
                checkPass: [
                    { validator: validatePass2, trigger: 'blur' }
                ],
                roleId: [
                    { validator: validaterolre, trigger: 'blur' }
                ]
            }
        };
    },
    created() {
      this.findRole();
      if(this.id!=null){
          this.getAdmin();

      }
    },
    methods: {
        checkName(){
             this.$axios.post("manage/checkname/",this.adminFrom).then(res=>{
                 if(res.data.admin==null){
                 }
                 else {
                     this.$message.error("账号已存在")
                     return false;
                 }
             })

        },
        submitForm(ruleForm) {
            if (!this.adminFrom.adminUsername) {
              this.$message.error("请输入账号")
                return false;
            }else if(!this.adminFrom.adminPassword){
                this.$message.error("请输入密码")
                return false;
            }
            else if(!this.value){
                this.$message.error("请选择权限")
                return false;
            }
            else {
                this.adminFrom.roleId = this.value;
                this.$axios.post("manage/addadminchangepassword/", this.adminFrom).then(res => {
                    if (res.mse = "添加成功") {
                        alert("添加成功")
                        this.adminFrom = {}
                        this.value = ""
                        this.$router.push("admin")
                    }
                })
            }
        },
        resetForm(ruleForm) {
            this.$refs[ruleForm].resetFields();
        },
        findRole(){
            this.$axios.get("manage/findroles")
                .then(res=>{
                     this.options=res.data.adminroles
                })
        },
        getAdmin(){
            this.$axios.get("manage/getid?id="+this.id)
                .then(res=>{
                    this.adminFrom = res.data.admin;

            })
        }
    }
}
</script>

<style scoped>

</style>